package com.java1412.service.impl;

import com.java1412.entity.Cart;
import com.java1412.entity.Product;
import com.java1412.mapper.CartMapper;
import com.java1412.mapper.ProductMapper;
import com.java1412.service.CartService;
import com.java1412.utils.ResponseUtil;
import com.java1412.vo.CartVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author java1412
 * @since 2023-08-10 09:26:47
 */
@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Resource
    private CartMapper cartMapper;

    @Resource
    private ProductMapper productMapper;

    @Override
    public ResponseUtil findCountByUserId(Integer userId) {
        Integer count = cartMapper.findCountByUserId(userId);
        return ResponseUtil.ok("查询购物车数量成功", count);
    }

    @Override
    public ResponseUtil findByUserId(Integer userId) {
        // 获取购物车信息
        List<Cart> cartList = cartMapper.findByUserId(userId);
        // 对cartList循环，转换成cartVoList
        List<CartVo> cartVoList = new ArrayList<>();
        for (Cart cart : cartList) {
            CartVo cartVo = new CartVo();
            // 将cart中属性的值复制到cartVo中同名属性中
            BeanUtils.copyProperties(cart, cartVo);
            // 还有一个属性product没有值，根据cart中productId获取商品信息
            Product product = productMapper.findById(cart.getProductId());
            // 将商品对象设置到cartVo中的product属性
            cartVo.setProduct(product);
            // 将cartVo添加到cartVoList中
            cartVoList.add(cartVo);
        }
        return ResponseUtil.ok("获取购物车数据成功", cartVoList);
    }

    @Override
    public ResponseUtil updateCart(Integer id, Integer quantity, Integer checked) {
        ResponseUtil r = null;
        // 封装参数
        Map<String, Object> map = new HashMap();
        map.put("id", id);
        map.put("quantity", quantity);
        map.put("checked", checked);
        // 执行更新
        Integer result = cartMapper.updateCart(map);
        r = result.intValue() > 0 ?
                ResponseUtil.ok("更新购物车成功") : ResponseUtil.fail("更新购物车失败");
        return r;
    }

    @Override
    public ResponseUtil deleteById(Integer id) {
        Integer result = cartMapper.deleteById(id);
        ResponseUtil r = result.intValue() > 0 ?
                ResponseUtil.ok("删除购物车成功") : ResponseUtil.fail("删除购物车失败");
        return r;
    }

    @Override
    public ResponseUtil deleteBatch(List<Integer> ids) {
        Integer result = cartMapper.deleteBatch(ids);
        return result.intValue() > 0 ?
                ResponseUtil.ok("批量删除购物车成功") : ResponseUtil.fail("批量删除购物车失败");
    }

    @Override
    public ResponseUtil deleteByUserId(Integer userId) {
        return cartMapper.deleteByUserId(userId).intValue() > 0 ?
                ResponseUtil.ok("删除购物车成功") : ResponseUtil.fail("删除购物车失败");
    }
}
